<?php

class Berita extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('admin/m_berita');
        $this->load->library('pagination');
		$this->load->helper('tgl_indonesia');
		

	}
	
	function index(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_berita->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/berita/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$data['tabel'] = $this->m_berita->getTable($config['per_page'], $this->uri->segment(4));
			$data['main_view'] = 'admin/berita/index';
			$this->load->view('admin/template', $data);
		}
	}
	
	function add(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$data['main_view'] = 'admin/berita/add';
			$this->load->view('admin/template', $data);
		}
	}
	
	function insert(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('judul', 'Judul Berita', 'required');
			$this->form_validation->set_rules('berita', 'Isi Berita', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			if($this->form_validation->run()==true){
				$judul = $this->input->post('judul');
				$berita = $this->input->post('berita');
				$tgl = date("Y-m-d");
				$hari = nama_hari($tgl);
				$seo = str_replace(" ", "-", $judul);
				$judul_seo = strtolower($seo . ".html");
				$jam = gmdate("H:i:s", time()+60*60*7);
				$username = $this->session->userdata('username');
				$gbr=$_FILES['userfile']['name'];
				$acak=rand(00000000000,99999999999);
				$gbr_default = 'default.jpg';
				$gbr_bru = $acak.$gbr;
				
				if(!empty($gbr)){
					$config['upload_path'] = './image/gambar_berita/';
					$config['allowed_types'] = 'bmp|gif|jpg|jpeg|png';
					$config['max_size'] = '2000';
					$config['max_width'] = '1020';
					$config['max_height'] = '768';	
					$config['file_name']= $gbr_bru;				
					$this->load->library('upload', $config);
				
					if(!$this->upload->do_upload())
					{
						echo $this->upload->display_errors();
					}else{
						$this->m_berita->create($username,$judul,$judul_seo,$berita,$gbr_bru,$hari,$tgl,$jam, 1);
						redirect('admin/berita');
					}
				}else{
					$this->m_berita->create($username,$judul,$judul_seo,$berita,$gbr_default,$hari,$tgl,$jam, 1);
					redirect('admin/berita');
				}
			}else{
				$data['main_view'] = 'admin/berita/add';
				$this->load->view('admin/template', $data);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function edit(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			//$id = $this->uri->segment();
			$data['data'] = $this->m_berita->getDataById($this->uri->segment(4));
			$data['main_view'] = 'admin/berita/edit';
			$this->load->view('admin/template', $data);
		}
	}
	
	function update($id){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('judul', 'Judul Berita', 'required');
			$this->form_validation->set_rules('berita', 'Isi Berita', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			$id = $this->input->post('id_berita');
			if($this->form_validation->run()==true){
				$judul = $this->input->post('judul');
				$berita = $this->input->post('berita');
				$seo = str_replace(" ", "-", $judul);
				$judul_seo = strtolower($seo . ".html");
				$gbr=$_FILES['userfile']['name'];
				$gbr_lama = $this->input->post('gambar');
				$acak=rand(00000000000,99999999999);
				$gbr_baru = $acak.$gbr;
				
				if(!empty($gbr)){
					$config['upload_path'] = './image/gambar_berita/';
					$config['allowed_types'] = 'bmp|gif|jpg|jpeg|png';
					$config['max_size'] = '2000';
					$config['max_width'] = '1020';
					$config['max_height'] = '768';	
					$config["file_name"]=$gbr_baru;			
					$this->load->library('upload', $config);
				
					if(!$this->upload->do_upload())
					{
						echo $this->upload->display_errors();
					}else{
						$file = './image/gambar_berita/' . $gbr_lama;
						unlink($file);
						$id = $this->input->post('id_berita');
						$this->m_berita->update($id,$judul,$judul_seo,$berita,$gbr_baru);
						redirect('admin/berita');
					}
				}else{
					$id = $this->input->post('id_berita');
					$this->m_berita->update($id,$judul,$judul_seo,$berita,$gbr_lama);
					redirect('admin/berita');
				}
			}else{
				
				redirect('admin/berita/edit/' . $id);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function delete(){
		$id = $this->uri->segment(4);
		$gbr= $this->uri->segment(5);
		$file = './image/gambar_berita/'.$gbr;
		if($gbr!='default.jpg'){
			unlink($file);
		}
		$this->m_berita->delete($id);
		redirect('admin/berita');
	}
	
	function search(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_berita->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/berita/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$judul = $this->input->post('judul');
			
			$data['tabel'] = $this->m_berita->search($judul);
			$data['main_view'] = 'admin/berita/index';
			$this->load->view('admin/template', $data);
		}	
	}
}